package com.nolon.flange.tools;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;

import androidx.annotation.RequiresApi;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.ArrayList;

import com.nolon.flange.model.FileItem;

public class FileTools {

    /**
     * 查找文件
     *
     * @return
     */
    public static ArrayList<FileItem> getAllXls(Context context) {

        ArrayList<FileItem> files = new ArrayList<>();

        String[] projection = new String[0];
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
            projection = new String[]{MediaStore.Files.FileColumns._ID,
                    MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.TITLE,
                    MediaStore.Files.FileColumns.MIME_TYPE, MediaStore.Files.FileColumns.SIZE, MediaStore.Files.FileColumns.RELATIVE_PATH
                    , MediaStore.Files.FileColumns.DATE_MODIFIED};
        }


        //相当于我们常用sql where 后面的写法
        String selection = MediaStore.Downloads.MIME_TYPE + "= ? ";
//                + " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? ";
        Uri uri = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            uri = MediaStore.Downloads.getContentUri(
                    MediaStore.VOLUME_EXTERNAL
            );
        }
//        else {
//            uri = MediaStore.Downloads.EXTERNAL_CONTENT_URI;
//        }

//        String[] selectionArgs = new String[]{"application/msword", "text/plain", "application/pdf", "application/vnd.ms-powerpoint", "application/vnd.ms-excel"};
        String[] selectionArgs = new String[]{"application/vnd.ms-excel"};

        Cursor cursor = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
            cursor = context.getContentResolver().query(uri,
                    projection,
                    selection,
                    selectionArgs,
                    MediaStore.Files.FileColumns.DATE_ADDED + " DESC");
        }

        while (cursor.moveToNext()) {

            String path = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA));
            File f = new File(path);
            FileItem fileItem = new FileItem(new File(path));
            fileItem.setUri(ContentUris.withAppendedId(uri, cursor.getLong(cursor.getColumnIndex(MediaStore.Files.FileColumns._ID))));
            fileItem.setName(cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.TITLE)));
            fileItem.setPath(cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA)));
            fileItem.setSize(cursor.getLong(cursor.getColumnIndex(MediaStore.Files.FileColumns.SIZE)));
            fileItem.setType(cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.MIME_TYPE)));
            files.add(fileItem);

        }
        cursor.close();
        return files;
    }

    @RequiresApi(api = Build.VERSION_CODES.Q)
    public static Uri getExcelContentUri(@NotNull Context context, @NotNull File file) {

        String[] projection = new String[]{MediaStore.Files.FileColumns._ID,
                MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.TITLE,
                MediaStore.Files.FileColumns.MIME_TYPE, MediaStore.Files.FileColumns.SIZE, MediaStore.Files.FileColumns.DATE_MODIFIED, MediaStore.Files.FileColumns.MIME_TYPE};
        String selection = MediaStore.Files.FileColumns.DATA + "=? ";

        String[] selectionArgs = new String[]{file.getAbsolutePath()};

        Cursor cursor = context.getContentResolver().query(MediaStore.Files.getContentUri("external"),
                projection, selection, selectionArgs, null);
        Uri uri = null;

        if (cursor != null) {
            if (cursor.moveToFirst()) {
                int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Files.FileColumns._ID));
                Uri baseUri = Uri.parse("content://media/external/download");
                uri = Uri.withAppendedPath(baseUri, "" + id);
            }
            cursor.close();
        }

        if (uri == null) {
            ContentValues values = new ContentValues();
            values.put(MediaStore.Files.FileColumns.DATA, file.getAbsolutePath());
            uri = context.getContentResolver().insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, values);
        }

        return uri;
    }
}
